package com.wty.shop.controller.backend;

import com.wty.shop.common.Const;
import com.wty.shop.common.ServerResponse;
import com.wty.shop.pojo.User;
import com.wty.shop.service.IUserService;
import com.wty.shop.util.CookieUtil;
import com.wty.shop.util.JsonUtil;
import com.wty.shop.util.RedisShardedPoolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * @Author 34355
 * @Create 2018-01-07-22:28
 */
@Controller
@RequestMapping("/manage/user")
public class UserManageController {
    @Autowired
    private IUserService iUserService;

    /**
     * 后台管理员登录
     * @param username
     * @param password
     * @param session
     * @return
     */
    @RequestMapping(value="login.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> login(String username, String password, HttpSession session, HttpServletResponse httpServletResponse){
        ServerResponse<User> response = iUserService.login(username,password);
        if(response.isSuccess()){
            User user = response.getData();

            if(iUserService.checkAdminRole(user).isSuccess()){
                //说明登录的是管理员
//                session.setAttribute(Const.CURRENT_USER,user);
                CookieUtil.writeLoginToken(httpServletResponse,session.getId());
                RedisShardedPoolUtil.setEx(session.getId(), Const.RedisCacheExtime.REDIS_SESSION_EXTIME,JsonUtil.obj2String(response.getData()));
                return response;
            }else{
                return ServerResponse.createByErrorMessage("不是管理员,无法登录");

            }
        }
        return response;
    }
}
